# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS                       ####
# ################################################################# #

### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)

### Set working directory to the folder of this script
apollo_setWorkDir()

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelDescr      = "Fractional MNL model on time use data",
  indivID         = "indivID", 
  outputDirectory = "output"
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
# ################################################################# #

### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv), 
### the code would be: database = read.csv("data.csv",header=TRUE)
database = apollo_timeUseData
### for data dictionary, use ?apollo_timeUseData

### Convert time use into percentage of a day
tmp = grep("^t_a", names(database), value=TRUE)
database[,tmp] = database[,tmp]/database$budget
rm(tmp)

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_dropOff      = 0,
              asc_work         = 0,
              asc_school       = 0,
              asc_shopping     = 0,
              asc_privBusiness = 0,
              asc_petrol       = 0,
              asc_leisure      = 0,
              asc_vacation     = 0,
              asc_exercise     = 0,
              asc_home         = 0,
              asc_travel       = 0,
              asc_other        = 0)

### Vector with names (in quotes) of parameters to be kept fixed at their starting value in apollo_beta, use apollo_fixed = c() if none
apollo_fixed = c("asc_other")

# ################################################################# #
#### GROUP AND VALIDATE INPUTS                                   ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION                        ####
# ################################################################# #

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){

  ### Attach inputs and detach after function exit
  apollo_attach(apollo_beta, apollo_inputs)
  on.exit(apollo_detach(apollo_beta, apollo_inputs))

  ### Create list of probabilities P
  P = list()
  
  ### List of utilities: these must use the same names as in fmnl_settings, order is irrelevant
  V = list()
  V[["dropOff"     ]] = asc_dropOff 
  V[["work"        ]] = asc_work    
  V[["school"      ]] = asc_school  
  V[["shopping"    ]] = asc_shopping
  V[["privBusiness"]] = asc_privBusiness
  V[["petrol"      ]] = asc_petrol  
  V[["leisure"     ]] = asc_leisure 
  V[["vacation"    ]] = asc_vacation
  V[["exercise"    ]] = asc_exercise
  V[["home"        ]] = asc_home    
  V[["travel"      ]] = asc_travel  
  V[["other"       ]] = asc_other
  
  ### Define settings for MNL model component
  fmnl_settings = list(
    alternatives  = c("dropOff", 
                      "work", 
                      "school", 
                      "shopping", 
                      "privBusiness", 
                      "petrol", 
                      "leisure", 
                      "vacation", 
                      "exercise", 
                      "home", 
                      "travel", 
                      "other"), 
    choiceShares  = list(dropOff   = t_a01, 
                      work         = t_a02, 
                      school       = t_a03, 
                      shopping     = t_a04, 
                      privBusiness = t_a05, 
                      petrol       = t_a06, 
                      leisure      = t_a07, 
                      vacation     = t_a08, 
                      exercise     = t_a09, 
                      home         = t_a10, 
                      travel       = t_a11, 
                      other        = t_a12), 
    utilities     = V
  )
  
  ### Compute probabilities using FMNL model
  P[["model"]] = apollo_fmnl(fmnl_settings, functionality)

  ### Take product across observation for same individual
  P = apollo_panelProd(P, apollo_inputs, functionality)

  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}

# ################################################################# #
#### MODEL ESTIMATION                                            ####
# ################################################################# #

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)

# ################################################################# #
#### MODEL OUTPUTS                                               ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN)                               ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model)

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name)               ----
# ----------------------------------------------------------------- #

apollo_saveOutput(model)


